<template>
	<view>
		<view v-for="(item,index) in article" :key="item.id">
			<navigator :url="'../../forum/articleinfo?id='+item.id">
				{{ item.id }} - {{ item.title }} - {{ item.content }} 
			</navigator>
			<view :data-id="item.id" @click="delarticle">删除</view>
			<navigator :url="'./form?id='+item.id">
				编辑
			</navigator>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				article:[],
				current_page:1,
				last_page:1,
				perPage:30
			}
		},
		methods: {
			articleList(page,perPage){
				this.api.request({url:'/api/v1/article/myindex',method:'GET',formdata:{page:page,perPage:perPage},
				}).then(res=>{
					if(res.code==800){
						if(page==1){
							this.article = res.data.data;
						}else{
							this.article = this.article.concat(res.data.data);
						}
						this.current_page = res.data.current_page;
						this.last_page = res.data.last_page;
					}
				});
			},
			delarticle(e){
				var id = e.currentTarget.dataset.id
				this.api.request({url:'/api/v1/article/'+id,method:'POST',formdata:{_method:'DELETE'}
				}).then(res=>{
					console.log(res);
					if(res.code==800){
						var index0 = this.article.findIndex(item => {
						    if (item.id == id) {
								return true;
						    }
						});
						this.article.splice(index0, 1);
					}
				});
			}
		},
		onReady(){
			this.articleList(this.current_page,this.perPage);
		},
		onReachBottom(){
			var page = this.current_page+1;
			if(page<=this.last_page){
				this.articleList(page,this.perPage);
			}
		},
		onPullDownRefresh(){
			this.articleList(1,this.perPage);
		}
	}
</script>

<style>

</style>
